<?php /* Smarty version 2.6.20, created on 2013-05-25 01:44:44
         compiled from _core/_include/common/miscutils.tpl */ ?>
function MiscUtils() {
};

MiscUtils.conjunction = 1;
MiscUtils.disjunction = 2;

MiscUtils.unique = function() {
  var unique = 'u' + parseInt(new Date().getTime() * Math.random());
  return unique;
};

MiscUtils.encode = function(obj) {
  var str = JSON.stringify(obj);
  var result = '';
  for (i = 0, il = str.length; i < il; i++) {
    result += this.digits(str.charCodeAt(i), 3);
  }
  return result;
};

MiscUtils.decode = function(str) {
  if (str == '') {
    return {};
  }
  
  var result = '';
  for (i = 0, il = str.length; i < il; i += 3) {
    result += String.fromCharCode(str.substring(i, i + 3));
  }
  return JSON.parse(result);
};

MiscUtils.digits = function(value, size) {
  var prefix = '';
  for (var i = 0, il = size - value.toString().length; i < il; i++) {
    prefix += '0';
  }
  return prefix + value;
};

MiscUtils.clone = function(object) {
//console.log(object);
  //return JSON.parse(JSON.stringify(object));
  return object;
};

MiscUtils.span = function(content) {
  span = document.createElement('span');
  var str = '';
  for (var i = 0, il = content.length; i < il; i++) {
    if (content.charAt(i) != '\n') {
      str += content.charAt(i);
    } else {
      span.appendChild(document.createTextNode(str));
      span.appendChild(document.createElement('br'));
      str = '';
    }
  }
  span.appendChild(document.createTextNode(str));
  return span
};

MiscUtils.dialog = function(popupBox, pos, content, func1, func2, options) {
  var table, tr, td, btn;
  if (options && options.pop) {
    table = document.createElement('table');
    table.cellPadding = 0;
    table.cellSpacing = 0;
    popupBox._open(table, { pos: pos, t: (options && options.t) ? options.t : '', width: (options && options.width) ? options.width : null, height: (options && options.height) ? options.height : null, pop: (options && options.pop) ? options.pop : null });
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(content);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    if (func1 || func2) {
      if (func1) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.ok) ? options.ok : 'OK', null);
        btn.style.width = '75px';
        btn.onclick = func1;
        td.appendChild(btn);
      }
      if (func2) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.cancel) ? options.cancel : 'Cancel', null);
        btn.style.width = '75px';
        btn.onclick = func2;
        td.appendChild(btn);
      }
    }
  } else {
    table = document.createElement('table');
    popupBox._open(table, { pos: pos, t: (options && options.t) ? options.t : '', width: (options && options.width) ? options.width : null, height: (options && options.height) ? options.height : null, pop: (options && options.pop) ? options.pop : null, func: (options && options.func) ? options.func : null });
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(content);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.padding = '10px 0 2px 0';
    td.style.textAlign = 'center';
    if (func1 || func2) {
      if (func1) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.ok) ? options.ok : 'OK', null);
        btn.style.width = '75px';
        btn.onclick = func1;
        td.appendChild(btn);
      }
      if (func2) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.cancel) ? options.cancel : 'Cancel', null);
        btn.style.width = '75px';
        btn.onclick = func2;
        td.appendChild(btn);
      }
    }
  }
};

MiscUtils.dialogtop = function(popupBox, pos, content, func1, func2, options) {
  var table, tr, td, btn;
  if (options && options.pop) {
    table = document.createElement('table');
    table.cellPadding = 0;
    table.cellSpacing = 0;
    popupBox._open(table, { pos: pos, t: (options && options.t) ? options.t : '', width: (options && options.width) ? options.width : null, height: (options && options.height) ? options.height : null, pop: (options && options.pop) ? options.pop : null });

    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    if (func1 || func2) {
      if (func1) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.ok) ? options.ok : 'OK', null);
        btn.style.width = '75px';
        btn.onclick = func1;
        td.appendChild(btn);
      }
      if (func2) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.cancel) ? options.cancel : 'Cancel', null);
        btn.style.width = '75px';
        btn.onclick = func2;
        td.appendChild(btn);
      }
    }
  } else {
    table = document.createElement('table');
    popupBox._open(table, { pos: pos, t: (options && options.t) ? options.t : '', width: (options && options.width) ? options.width : null, height: (options && options.height) ? options.height : null, pop: (options && options.pop) ? options.pop : null, func: (options && options.func) ? options.func : null });
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(content);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.padding = '10px 0 2px 0';
    td.style.textAlign = 'center';
    if (func1 || func2) {
      if (func1) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.ok) ? options.ok : 'OK', null);
        btn.style.width = '75px';
        btn.onclick = func1;
        td.appendChild(btn);
      }
      if (func2) {
        btn = DOMUtils.createInput(null, null, 'button', (options && options.cancel) ? options.cancel : 'Cancel', null);
        btn.style.width = '75px';
        btn.onclick = func2;
        td.appendChild(btn);
      }
    }
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.appendChild(content);
  }
};

MiscUtils.alert = function(popupBox, pos, text) {
  var div = document.createElement('div');
  div.appendChild(document.createTextNode(text));
  div.style.margin = '0 0 10px 0';
  MiscUtils.dialog(popupBox, pos, div, function() { popupBox._close(); }, null);
};

MiscUtils.load = function(popupBox, pos, text) {
  var div = document.createElement('div');
  var img = document.createElement('img');
  img.src = 'images/common/loading.gif';
  img.style.margin = '0 0 5px 0';
  div.appendChild(img);
  div.appendChild(document.createElement('br'));
  div.appendChild(document.createTextNode(text));
  div.style.margin = '0 0 5px 0';
  MiscUtils.dialog(popupBox, pos, div, null, null);
};

/**
 * 找出数组中的某些元素
 */
MiscUtils.find = function(array, conditions, mode) {
  mode = (mode) ? mode : MiscUtils.conjunction;
  if (mode == MiscUtils.conjunction) {
    return this.conjoin(array, conditions);
  } else if (mode == MiscUtils.disjunction) {
    return this.disjoin(array, conditions);
  } else {
    return [];
  }
  
};

/**
 * 找出数组中符合所有条件的元素
 */
MiscUtils.conjoin = function(array, conditions) {
  var result = [];
  for (var i = 0, il = array.length; i < il; i++) {
    var tmp = array[i];
    var required = true;
    for (var j = 0, jl = conditions.length; j < jl; j++) {
      var condition = conditions[j];
      var value = tmp;
      for (var k = 0, kl = condition.attrs.length; k < kl; k++) {
        var attr = condition.attrs[k];
        value = value[attr];
      }
      if (condition.unequal) {
        if (value == condition.value) {
          required = false;
        }
      } else {
        if (value != condition.value) {
          required = false;
        }
      }
    }
    if (required) {
      result[result.length] = tmp;
    }
  }
  return result;
};

/**
 * 找出数组中符合任一条件的元素
 */
MiscUtils.disjoin = function(array, conditions) {
  var result = [];
  for (var i = 0, il = array.length; i < il; i++) {
    var tmp = array[i];
    for (var j = 0, jl = conditions.length; j < jl; j++) {
      var condition = conditions[j];
      var value = tmp;
      for (var k = 0, kl = condition.attrs.length; k < kl; k++) {
        var attr = condition.attrs[k];
        value = value[attr];
      }
      if (condition.unequal) {
        if (value != condition.value) {
          result[result.length] = tmp;
        }
      } else {
        if (value == condition.value) {
          result[result.length] = tmp;
        }
      }
    }
  }
  return result;
};

/**
 * 删除数组中的某个元素
 */
MiscUtils.remove = function(array, elem, attr) {
  for (var i = 0, il = array.length; i < il; i++) {
    var tmp = array[i];
    if ((!attr && elem.id == tmp.id) || (attr && elem[attr] == tmp[attr])) {
      array.splice(i, 1);
      break;
    }
  }
  return array;
};

/**
 * 为一个数组排序（按照某个属性的名字顺序）。
 */
MiscUtils.sort = function(array, attrs) {
  var func = function(a, b) { var v1 = a, v2 = b;
                              for (var i = 0, il = attrs.length; i < il; i++) {
                                v1 = v1[attrs[i]];
                                v2 = v2[attrs[i]]
                              }
                              
                              if (isNaN(v1) || isNaN(v2)) {
                              
                                if (v1 && v2 && (v1.toString().toLowerCase().valueOf() > v2.toString().toLowerCase().valueOf()) ) {
                                  return 1;
                                }
                              } else {
                                if (v1 && v2 &&  ( parseFloat(v1, 10) > parseFloat(v2, 10) ) ) {
                                  return 1;
                                }
                              }
                              return -1;
                            };
  array.sort(func);
};

MiscUtils.formatCurrency = function(num) {
  num = num.toString().replace(/\$|\,/g, '');
  if(isNaN(num)) {
    num = "0";
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  cents = num % 100;
  num = Math.floor(num / 100).toString();
  if(cents < 10) {
    cents = "0" + cents;
  }
  for (var i = 0; i < Math.floor((num.length-(1+i)) / 3); i++) {
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
  }
  return (((sign) ? ' ' : '-' ) + '$' + num + '.' + cents);
};

/*将给定的字符串中的单引号转义*/
 MiscUtils.formatstring =function(str){
     var RexStr = /\'/g;
     str = str.replace(RexStr,
         function(MatchStr){
             switch(MatchStr){
                 case "'":
                     return "\\'";
                     break;
                 default :
                     break;
             }
         }
     )
     return str;
}; 


/*去掉字符串中所有空格(包括中间空格,需要设置第2个参数为:g)*/
MiscUtils.Trim=function(str,is_global)

        {

            var result;

            result = str.replace(/(^\s+)|(\s+$)/g,"");

            if(is_global.toLowerCase()=="g")

            {

                result = result.replace(/\s/g,"");

             }

            return result;

};

MiscUtils.toUpper=function(str,is_global)
{
	var result='';
	var reg = /\b(\w)|\s(\w)/; 
	
    var reCat = /[.。]/;
    var arrdata = str.split(reCat);
    var strall='';
	   
    for (var i = 0; i < arrdata.length; i++)
    {

	    	if(is_global.toLowerCase()=="g")
	    	{
	    	  result += this.replaceReg(reg,arrdata[i]);
	    	}else{
	        	if(arrdata[i]!='')
	        	{
	    		result += this.replaceReg(reg,arrdata[i])+'.';
	    	    }
    	    }
    } 
    
    return result;
	
	
};

MiscUtils.replaceReg=function (reg,str)
{ 
	//str = str.toLowerCase(); 
	return str.replace(reg,function(m){return m.toUpperCase()}); 
}; 

/*把2012-01-01的日期转换成系统里面的时间*/
MiscUtils.fdate=function (date)
{ 
	//date =  date.cwd.replace(/-/g,'/'); 
	var strs=date.split("-");
	
	var gg={};
	gg.year = strs[0];
	gg.month = strs[1];
	gg.date = strs[2];
	gg.hours = 0;
	gg.minutes = 0;
	gg.seconds = 0;
	
	return gg; 
}; 

MiscUtils.autopwd = function() {
  var unique = 'u' + parseInt(new Date().getTime() * Math.random());
  unique = unique.substr(1,4)
  return unique;
};







